<?php

namespace App\Http\Controllers;

use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;

class UploadController extends Controller
{
    /**
     * 通用上传方法
     * @param
     * @return
     * @author ScorpionFong
     */
    public function file(Request $request)
    {
        try {
            $allow = ['png', 'jpg', 'jpeg'];
            $max = 10 * 1024 * 1024;
            $validateData = $this->validate($request, [
                'file' => ['required', 'mimes:' . implode(',', $allow), 'max:' . $max],
            ], [
                'file.required' => '请选择上传文件',
                'file.mimes' => '请上传' . implode(',', $allow) . '类型的文件',
                'file.max' => '文件大小不能超过10MB',
            ]);
            $path = Storage::put('public/bgimg/' . date('Y-m-d'), $validateData['file']);
            return $this->success([
                'path' => $path,
                'name' => $request->file('file')->getClientOriginalName(),
                'url' => asset('storage/' . Str::replace('public/', '', $path)),
            ]);
        } catch (ValidationException $validationException) {
            return $this->error($validationException->validator->errors()->first());
        } catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function filePdf(Request $request)
    {
        try {
            $allow = ['pdf'];
            $max = 30 * 1024 * 1024;
            $validateData = $this->validate($request, [
                'file' => ['required', 'mimes:' . implode(',', $allow), 'max:' . $max],
            ], [
                'file.required' => '请选择上传文件',
                'file.mimes' => '请上传' . implode(',', $allow) . '类型的文件',
                'file.max' => '文件大小不能超过30MB',
            ]);
            $path = Storage::put('public/bgimg/' . date('Y-m-d'), $validateData['file']);
            return $this->success([
                'path' => $path,
                'name' => $request->file('file')->getClientOriginalName(),
                'url' => asset('storage/' . Str::replace('public/', '', $path)),
            ]);
        } catch (ValidationException $validationException) {
            return $this->error($validationException->validator->errors()->first());
        } catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 通用上传方法
     * @param
     * @return
     * @author ScorpionFong
     */
    public function files(Request $request)
    {
        try {
            $allow = ['png', 'jpg', 'jpeg'];
            $max = 10 * 1024 * 1024;
            $validateData = $this->validate($request, [
                'file.*' => ['required', 'mimes:' . implode(',', $allow), 'max:' . $max],
            ], [
                'file.*.required' => '请选择上传文件',
                'file.*.mimes' => '请上传' . implode(',', $allow) . '类型的文件',
                'file.*.max' => '文件大小不能超过10MB',
            ]);
            $data = [];
            foreach ($validateData['file'] as $file) {
                $path = Storage::put('public/bgimg/' . date('Y-m-d'), $file);
                $data[] = [
                    'path' => $path,
                    'name' => $file->getClientOriginalName(),
                    'url' => asset('storage/' . Str::replace('public/', '', $path)),
                ];

            }
            return $this->success($data);
        } catch (ValidationException $validationException) {
            return $this->error($validationException->validator->errors()->first());
        } catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function video(Request $request)
    {
        try {
            $allow = ['mp4', 'avi', 'mov'];
            $max = 100 * 1024 * 1024;
            $validateData = $this->validate($request, [
                'file' => ['required', 'mimes:' . implode(',', $allow), 'max:' . $max],
            ], [
                'file.required' => '请选择上传文件',
                'file.mimes' => '请上传' . implode(',', $allow) . '类型的文件',
                'file.max' => '文件大小不能超过100MB',
            ]);
            $path = Storage::put('public/video/' . date('Y-m-d'), $validateData['file']);
            return $this->success([
                'path' => $path,
                'name' => $request->file('file')->getClientOriginalName(),
                'url' => asset('storage/' . Str::replace('public/', '', $path)),
            ]);
        } catch (ValidationException $validationException) {
            return $this->error($validationException->validator->errors()->first());
        } catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function videoWang(Request $request)
    {
        try {
            $allow = ['mp4', 'avi', 'mov'];
            $max = 100 * 1024 * 1024;
            $validateData = $this->validate($request, [
                'file' => ['required', 'mimes:' . implode(',', $allow), 'max:' . $max],
            ], [
                'file.required' => '请选择上传文件',
                'file.mimes' => '请上传' . implode(',', $allow) . '类型的文件',
                'file.max' => '文件大小不能超过100MB',
            ]);
            $path = Storage::put('public/video/' . date('Y-m-d'), $validateData['file']);
            return $this->success([
                'path' => $path,
                'name' => $request->file('file')->getClientOriginalName(),
                'url' => asset('storage/' . Str::replace('public/', '', $path)),
            ], [
                'errno' => 0
            ]);
        } catch (ValidationException $validationException) {
            return $this->success([
                'message' => $validationException->validator->errors()->first(),
            ], [
                'errno' => 1
            ]);
        } catch (Exception $exception) {
            return $this->success([
                'message' => $exception->getMessage(),
            ], [
                'errno' => 1
            ]);
        }
    }


    public function imageWang(Request $request)
    {
        try {
            $allow = ['jpg', 'jpeg', 'png'];
            $max = 3 * 1024 * 1024;
            $files = $request->allFiles();
            if (is_array($files['file'])) {
                $this->validate($request, [
                    'file.0' => ['required', 'image', 'mimes:' . implode(',', $allow), 'max:' . $max],
                ], [
                    'file.0.required' => '请选择上传文件',
                    'file.0.image' => '请上传一个有效的资源',
                    'file.0.mimes' => '请上传' . implode(',', $allow) . '类型的文件',
                ]);
                $path = [];
                foreach ($files['file'] as $file) {
                    $path1 = Storage::put('public/bgimg/' . date('Y-m-d'), $file);
                    $path[] = [
                        'url' => asset('storage/' . Str::replace('public/', '', $path1)),
                        'alt' => '',
                        'href' => ''
                    ];
                }
                return $this->success($path);
            }
        } catch (ValidationException $validationException) {
            return $this->error($validationException->validator->errors()->first());
        } catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}
